/**
 * @class   wosh::IWidgetSerializable
 * @brief   Graphical interface of wosh::ISerializable
 *          
 *          
 *
 ****************************************************************************
 * @version $Id: IWidgetSerializable.h 2809 2010-07-30 05:20:06Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_IWidgetSerializable_H__
 #define __WOSH_Gui_IWidgetSerializable_H__

 #include <woshDefs.h>
 #include <core/Serialization.h>

class QWidget;

namespace wosh {
 namespace gui {

#undef WOSH_WIDGET_SERIALIZABLE
#define WOSH_WIDGET_SERIALIZABLE(WIDGET_CLASSNAME)				\
		WOSH_CLASSNAME(WIDGET_CLASSNAME)						\
		WOSH_CLASS_ABSTRACT(false)								\
																\
		public: inline QWidget* asQWidget() { return this; }



class IWidgetSerializable :  public virtual IReflection
 {
	WOSH_CLASSNAME(wosh::gui::IWidgetSerializable)
	WOSH_CLASS_ABSTRACT(true)


/** @name IWidgetSerializable interface
 * @{
 ******************************************************************************/
	public:
		virtual QWidget* asQWidget() = 0;

	public:
		virtual const char* getSerializableType() const = 0;

		virtual WRESULT loadSerializable( const ISerializable* object ) = 0;

		virtual WRESULT saveSerializable( ISerializable* object ) const = 0;
		virtual ISerializable* saveSerializable() const = 0;

		virtual void setReadOnly( bool read_only ) = 0;

//@}
/** @name Getters and Setters
 * @{
 ******************************************************************************/
	public:
		virtual inline bool isSerializableSupported( const ISerializable* object ) {
			if ( object == NULL ) return false;
			if ( !object->isClass(getSerializableType()) ) return false;
			return true;
		 }

		virtual inline bool isSerializableType( const char* classname ) const {
			if ( classname == NULL ) return false;
			return ( strcmp(getSerializableType(), classname ) == 0 );
		 }

//@}
	public:
		virtual ~IWidgetSerializable() { }


}; // class def


// ----------------------------------------------------------------------------------------


template<class OBJECT_TYPE>
class IWidgetSerializableT : public virtual IWidgetSerializable
 {

	public:
		inline const char* getSerializableType() const		{ return OBJECT_TYPE::className(); }

#ifdef _USE_COVARIANT_RETURN_TYPE
		virtual inline OBJECT_TYPE* newObject() const		{ return new OBJECT_TYPE(); }
#else
		virtual inline ISerializable* newObject() const		{ return new OBJECT_TYPE(); }
#endif

	public:
		virtual WRESULT loadObject( const OBJECT_TYPE* object ) = 0;
		virtual WRESULT saveObject( OBJECT_TYPE* object ) const = 0;

		virtual void setReadOnly( bool read_only ) {
			if ( asQWidget() == NULL  ) return;
			asQWidget()->setEnabled(!read_only);
		 }

		virtual inline WRESULT loadSerializable( const ISerializable* object ) {
			if ( object == NULL ) return loadObject(NULL);
			if ( !object->isKindOf<OBJECT_TYPE>() ) return WRET_ERR_PARAM;
			const OBJECT_TYPE* obj = object->as<OBJECT_TYPE>();
			return loadObject(obj);
		 }

		virtual inline WRESULT saveSerializable( ISerializable* object ) const {
			if ( object == NULL ) return saveObject(NULL);
			if ( !object->isKindOf<OBJECT_TYPE>() ) return WRET_ERR_PARAM;
			OBJECT_TYPE* obj = object->as<OBJECT_TYPE>();
			return saveObject(obj);
		 }

#ifdef _USE_COVARIANT_RETURN_TYPE
		inline virtual OBJECT_TYPE* saveSerializable() const {
			OBJECT_TYPE* obj = newObject();
			saveObject(obj);
			return obj;
		 }
#else
		inline virtual ISerializable* saveSerializable() const {
			ISerializable* obj = newObject();
			saveObject(obj);
			return obj;
		 }
#endif


	public:
		virtual ~IWidgetSerializableT() { }

}; // class def


// ----------------------------------------------------------------------------------------


template<class OBJECT_TYPE>
class IWidgetSerializableTA : public virtual IWidgetSerializable
 {

	public:
		inline const char* getSerializableType() const		{ return OBJECT_TYPE::className(); }

#ifdef _USE_COVARIANT_RETURN_TYPE
		virtual inline OBJECT_TYPE* newObject() const		{ return NULL; }
#else
		virtual inline ISerializable* newObject() const		{ return NULL; }
#endif

	public:
		virtual WRESULT loadObject( const OBJECT_TYPE* object ) = 0;
		virtual WRESULT saveObject( OBJECT_TYPE* object ) const = 0;

		virtual void setReadOnly( bool read_only ) {
			if ( asQWidget() == NULL  ) return;
			asQWidget()->setEnabled(read_only);
		 }

		virtual inline WRESULT loadSerializable( const ISerializable* object ) {
			if ( object == NULL ) return loadObject(NULL);
			if ( !object->isKindOf<OBJECT_TYPE>() ) return WRET_ERR_PARAM;
			const OBJECT_TYPE* obj = object->as<OBJECT_TYPE>();
			return loadObject(obj);
		 }

		virtual inline WRESULT saveSerializable( ISerializable* object ) const {
			if ( object == NULL ) return saveObject(NULL);
			if ( !object->isKindOf<OBJECT_TYPE>() ) return WRET_ERR_PARAM;
			OBJECT_TYPE* obj = object->as<OBJECT_TYPE>();
			return saveObject(obj);
		 }

#ifdef _USE_COVARIANT_RETURN_TYPE
		inline virtual OBJECT_TYPE* saveSerializable() const {
			return NULL;
		 }
#else
		inline virtual ISerializable* saveSerializable() const {
			return NULL;
		 }
#endif


	public:
		virtual ~IWidgetSerializableTA() { }

}; // class def



 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Gui_IWidgetSerializable_H__
